package com.unitedvision.persistence.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.unitedvision.core.domain.CompanyModule;
import com.unitedvision.core.exception.WrongFormatException;

@Entity
@Table (name = "company_module")
public class CompanyModuleEntity {
	private int id;
	private CompanyEntity company;
	private ModuleEntity module;
	private Date startDate;
	
	public CompanyModuleEntity() {
		super();
	}

	public CompanyModuleEntity(CompanyModule companyModule) throws WrongFormatException {
		super();
		this.id = companyModule.getId();
		this.company = CompanyEntity.create(companyModule.getCompany());
		this.module = ModuleEntity.create(companyModule.getModule());
		this.startDate = companyModule.getStartDate();
	}

	public CompanyModuleEntity(CompanyEntity company, ModuleEntity module, Date startDate) {
		super();
		this.company = company;
		this.module = module;
		this.startDate = startDate;
	}

	@Id @GeneratedValue
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@ManyToOne (fetch = FetchType.EAGER)
	@JoinColumn (name = "company_id", referencedColumnName = "id")
	public CompanyEntity getCompany() {
		return company;
	}

	public void setCompany(CompanyEntity company) {
		this.company = company;
	}

	@ManyToOne (fetch = FetchType.EAGER)
	@JoinColumn (name = "module_id", referencedColumnName = "id")
	public ModuleEntity getModule() {
		return module;
	}

	public void setModule(ModuleEntity module) {
		this.module = module;
	}

	@Temporal (TemporalType.DATE)
	@Column (name = "start_date")
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	public CompanyModule toCompany() {
		return new CompanyModule(id, company.toCompany(), module.toModule(), startDate);
	}
	
	public static CompanyModuleEntity create(CompanyEntity company, ModuleEntity module, Date startDate) {
		return new CompanyModuleEntity(company, module, startDate);
	}

	public static CompanyModuleEntity create(CompanyModule companyModule) throws WrongFormatException {
		return new CompanyModuleEntity(companyModule);
	}

	@Override
	public String toString() {
		return "CompanyModuleEntity [id=" + id + ", company=" + company
				+ ", module=" + module + ", startDate=" + startDate + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((company == null) ? 0 : company.hashCode());
		result = prime * result + id;
		result = prime * result + ((module == null) ? 0 : module.hashCode());
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CompanyModuleEntity other = (CompanyModuleEntity) obj;
		if (company == null) {
			if (other.company != null)
				return false;
		} else if (!company.equals(other.company))
			return false;
		if (id != other.id)
			return false;
		if (module == null) {
			if (other.module != null)
				return false;
		} else if (!module.equals(other.module))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		return true;
	}

}
